package com.xzgj.fmmall.controller;

import com.xzgj.fmmall.entity.UserAddr;
import com.xzgj.fmmall.service.UserAddrService;
import com.xzgj.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/userAddr")
@Api(value = "提供收货地址的相关接口",tags = "收货地址管理")

public class UserAddrController {

    @Resource
    private UserAddrService userAddrService;

    @ApiOperation("收货地址列表")
    @ApiImplicitParam(dataType = "int",name = "userId",value = "用户id",required = true)
    @GetMapping(value = "/list")
    public ResultVO listAddrs(Integer userId, @RequestHeader("token") String token){
        ResultVO resultVO = userAddrService.listAddrByUserId(userId);
        return resultVO;
    }

    @ApiOperation("添加收货地址")
    @PostMapping(value = "/add")
    public ResultVO insertAddr(@RequestBody UserAddr userAddr, @RequestHeader("token") String token){
        ResultVO resultVO = userAddrService.insertAddrByUserId(userAddr);
        return resultVO;
    }

    @ApiOperation("更新、修改收货地址")
    @PutMapping(value = "/info")
    public ResultVO updateAddr(@RequestBody UserAddr userAddr, @RequestHeader("token") String token){
        ResultVO resultVO = null;
        try {
            resultVO = userAddrService.updateAddrByUserId(userAddr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultVO;
    }

    @ApiOperation("根据addrId删除收货地址")
    @DeleteMapping(value = "/delByUserId")
    public ResultVO deleteAddr(String addrId, @RequestHeader("token") String token){
        ResultVO resultVO = null;
        try {
            resultVO = userAddrService.deleteAddrByAddrId(addrId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultVO;
    }

    @ApiOperation("设置默认收货地址")
    @PutMapping(value = "/updateByUserId")
    public ResultVO updateDefaultAddr(Integer aid,Integer uid, String username){
        return userAddrService.setDefault(aid, uid, username);
    }
}
